﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace produccion.clases
{
    class CLSTABPIE
    {
        string codpie_pie, nombre_pie;
        MDOgeneral mod = new MDOgeneral();

        public string CODPIE_PIE
        {
            get { return codpie_pie; }
            set { codpie_pie = value; }
        }

        public string NOMBRE_PIE
        {
            get { return nombre_pie; }
            set { nombre_pie = value; }
        }


        #region metodos

        public List<CLSTABPIE> listarPiezas()
        {

            try
            {
                string query = "select codpie_pie, nombre_pie from tabpie order by nombre_pie ";
                DataTable val = mod.consulta(query);
                List<CLSTABPIE> lispie = new List<CLSTABPIE>();
                foreach (DataRow r in val.Rows)
                {
                    lispie.Add(new CLSTABPIE
                    {
                        CODPIE_PIE = r[0].ToString(),
                        NOMBRE_PIE = r[1].ToString()
                    });
                }
                return lispie;
            }
            catch (Exception ex) { throw (ex); }
        }

        public DataTable consLisPie()
        {
            try
            {
                string query = "select TRIM(CODPIE_PIE) as CODPIE_PIE,TRIM(NOMBRE_PIE) as NOMBRE_PIE from tabpie order by NOMBRE_PIE";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }
        public int Insertar()
        {
            try
            {
                string query = "INSERT INTO TABPIE(CODPIE_PIE, NOMBRE_PIE) VALUES ('" + CODPIE_PIE + "','" + NOMBRE_PIE + "')";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public int Actualizar()
        {
            try
            {
                string query = "update tabpie set nombre_pie='" + NOMBRE_PIE + "' where codpie_pie='" + CODPIE_PIE + "'";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public int Eliminar()
        {
            try
            {
                string query = "delete from tabpie where codpie_pie='" + CODPIE_PIE + "'";
                return mod.comando(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public DataTable Consultar()
        {
            try
            {
                string query = "select codpie_pie, nombre_pie from tabpie ORDER BY NOMBRE_PIE";
                return mod.consulta(query);
            }
            catch (Exception ex) { throw ex; }
        }

        public Boolean Existe()
        {
            try
            {
                string query = "select codpie_pie from tabpie where codpie_pie='" + CODPIE_PIE + "'";
                if (mod.consulta(query).Rows.Count > 0)
                {
                    return true;
                }
                return false;
            }
            catch (Exception ex) { throw ex; }
        }    
        #endregion
        
    }
}
